#include <iostream>
using namespace std;

const int N=30010;
int arr[N], sign[N], _size[N];

int fd(int x)
{
	if(arr[x]==x) return x;
	
	int t=fd(arr[x]);
	sign[x]+=sign[arr[x]];
	return arr[x]=t;
}

int main()
{
	int T; cin>>T;
	
	for(int i=1; i<=N; i++)
	{
		arr[i]=i;
		_size[i]=1;	
	}
	while(T--)
	{
		char ch;
		int i, j;
		cin>>ch>>i>>j;
		
		int fx=fd(i), fy=fd(j);
		if(ch=='M' && fx!=fy)
		{
			arr[fx]=fy;
			
			sign[fx]+=_size[fy];
			_size[fy]+=_size[fx];
			_size[fx]=0;
		}
		else
		{
			if(fx!=fy) cout<<-1<<endl;
			else cout<< abs(sign[i]-sign[j])-1 <<endl;
		}
	}
	return 0;
 } 
